package win.tbs.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import win.tbs.boot.bean.Pet;
import win.tbs.boot.bean.User;
import win.tbs.boot.config.MyConfig;

/**
 * 主程序类
 * 主配置类
 * @SpringBootApplication：这是一个SpringBoot应用
 */
//@SpringBootApplication
/*
主程序所在包及其下面的所有子包里面的组件都会被默认扫描进来
无需以前的包扫描配置

• 想要改变扫描路径，@SpringBootApplication(scanBasePackages="com.atguigu")
• 或者@ComponentScan 指定扫描路径
*/
/*
    @SpringBootApplication
        等同于
    @SpringBootConfiguration
    @EnableAutoConfiguration
    @ComponentScan("com.atguigu.boot")
*/
@SpringBootConfiguration //就是 @Configuration 表示当前类为配置类
@EnableAutoConfiguration //重点
@ComponentScan("win.tbs.boot")  //指定组件扫描

public class MainApplication {

    public static void main(String[] args) {
        //1.返回我们的IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //2.查看容器中的组件
        String[] beanDefinitionNames = run.getBeanDefinitionNames();
        for (String name :
                beanDefinitionNames) {
            System.out.println(name);
        }

        //3、从容器中获取组件
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("组件：tom01 == tom02 ? "+(tom01 == tom02));

        //4.配置类本身也是组件; 默认@Configuration(proxyBeanMethods = true) 获取到的是代理对象
        MyConfig myConfig = run.getBean(MyConfig.class);
        System.out.println(myConfig);

        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
        User user = myConfig.user01();
        User user1 = myConfig.user01();
        System.out.println(user == user1);


        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);

        System.out.println("用户的宠物："+(user01.getPet() == tom));

        System.out.println(run.getBean("car"));
    }

}
